递归
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def check(self, left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
        if (left == None) and (right == None):
            return True
        elif ((left != None) and (right == None)) or \
            ((left == None) and (right != None)):
            return False
        elif left.val != right.val:
            return False
        else:
            return (self.check(left.left, right.right) == True) and \
                    (self.check(left.right, right.left) == True)
            
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        if root == None:
            return True
        else:
            return self.check(root.left, root.right)

迭代：
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def check(self, left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
        q = []
        q.extend([left, right])
        while len(q) > 0:
            left = q[0]
            q = q[1:]
            right = q[0]
            q = q[1:]
            if (left == None) and (right == None):
                continue
            if ((left == None) or (right == None)) or (left.val != right.val):
                return False
            q.append(left.left)
            q.append(right.right)
            q.append(left.right)
            q.append(right.left)
        
        return True
            
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        return self.check(root, root)
        